<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of db
 *
 * @author Loug S
 */
class db {
    //put your code here
    private $dsn;
    private $user;
    private $pass;
    private $pdo;
    private $pdoStatement;
    private static $instance;
    public static function getInstance($config) {
    if ( ! isset(self::$instance)) {
    self::$instance = new db($config);
    }
    return self::$instance;
    }
    public function __construct($config) {
    $this->dsn = $config['dsn'];
    $this->user = $config['user'];
    $this->pass = $config['pass'];
    }
    public function connect() {
    $this->pdo = new PDO($this->dsn, $this->user, $this->pass);
    }
    public function disconnect() {
    $this->pdo = null;
    }
    public function query($query, $params=array()) {
       
        if ($this->pdo === null) {
            $this->connect();
            }
        $this->pdoStatement = $this->pdo->prepare($query);
        $this->pdoStatement->setFetchMode(PDO::FETCH_ASSOC);
        $this->pdoStatement->execute($params);
    }
    public function lastId() {
    return $this->pdo->lastInsertId();
    }
    
    public function fetchAll() {
    return $this->pdoStatement->fetchAll();
    }

}

?>
